package com.scuec.groupfive.controller;

import com.scuec.groupfive.entity.ExamStatistics;
import com.scuec.groupfive.service.ExamStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/statistics")
@CrossOrigin
public class ExamStatisticsController {

    @Autowired
    private ExamStatisticsService examStatisticsService;

    /**
     * 获取考试统计数据（GET 请求，通过 URL 参数传 timeDimension）
     * 示例请求：/api/statistics/exam?timeDimension=day
     *
     * @param timeDimension 时间维度：day/week/month/year
     * @return 统计数据列表
     */
    @GetMapping("/exam")  // 从 @PostMapping 改为 @GetMapping
    public ResponseEntity<?> getExamStatistics(
            @RequestParam String timeDimension  // 从 @RequestBody 改为 @RequestParam
    ) {
        try {
            List<ExamStatistics> statistics = examStatisticsService.getExamStatistics(timeDimension);
            return ResponseEntity.ok(statistics);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("Failed to get exam statistics");
        }
    }
}